package top.yongyan0624.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import top.yongyan0624.Utils.OSSUtil;
import top.yongyan0624.common.lang.CodeMsg;
import top.yongyan0624.common.lang.ResultVO;

/**
 * @author Yongyan
 * @description
 * @date 2021/8/15
 **/

@RestController
@RequestMapping("/img")
public class ImgController extends BaseController {

    @PostMapping("/upload")
    @PreAuthorize("hasAnyRole('admin','superAdmin')")
    public ResultVO<String> modifyAvatar(MultipartFile img) {
        try {
            if (img.isEmpty()) {
                return ResultVO.error(new CodeMsg("图片路径为空"));
            }
            //存储文件，同时获取图片URL
            String url = OSSUtil.getImgUrl(img);
            return ResultVO.success(url);
        } catch (IllegalStateException e) {
            e.printStackTrace();
            return ResultVO.error(new CodeMsg("图片路径为空"));
        }

    }
}
